//
//  ToolUtils.m
//  quickBatchManager
//
//  Created by 王定波 on 15/9/17.
//  Copyright (c) 2015年 jack. All rights reserved.
//

#import "ToolUtils.h"

@implementation ToolUtils


+ (UIImage *)scaleToSize:(UIImage *)img{
    CGFloat width = img.size.width;
    CGFloat height = img.size.height;
    
    CGSize size;
    
    int max=600;
    
    
    if(width>max)
    {
        size=CGSizeMake(max, height*max/width);
    }
    else if(height>max)
        size=CGSizeMake(width*max/height, max);
    else return img;
    
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0,0, size.width, size.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    //返回新的改变大小后的图片
    return scaledImage;
}



+(UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}


+(NSString*) saveImageToLocal:(UIImage*)image
{
    NSString *date=[DateUtils dateToStringByFormat:[NSDate date] format:@"yyyyMMdd"];
    NSArray *paths = DOCUMENT_PATH;
    NSString *photoCacheDir = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",folerName_PHOTO,date]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:photoCacheDir isDirectory:nil])
    [fileManager createDirectoryAtPath:photoCacheDir withIntermediateDirectories:YES attributes:nil error:nil];
   
    NSString *fileName=[NSString stringWithFormat:@"%.0f.png",[[NSDate date] timeIntervalSince1970InMilliSecond]];;
    NSString *filePath = [NSString stringWithFormat:@"%@/%@",photoCacheDir,fileName];   // 保存文件的名称
    [UIImagePNGRepresentation(image) writeToFile: filePath    atomically:YES];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:EventImageSynchronization object:nil];
    
    return [NSString stringWithFormat:@"%@/%@",date,fileName];
}


+(NSString *)saveSmallImageToLocal:(UIImage *)image
{
    NSString *date=[DateUtils dateToStringByFormat:[NSDate date] format:@"yyyyMMdd"];
    NSArray *paths = DOCUMENT_PATH;
    NSString *photoCacheDir = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",folerName_PHOTO_SMALL,date]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:photoCacheDir isDirectory:nil])
        [fileManager createDirectoryAtPath:photoCacheDir withIntermediateDirectories:YES attributes:nil error:nil];
    
    NSString *fileName=[NSString stringWithFormat:@"%.0f.png",[[NSDate date] timeIntervalSince1970InMilliSecond]];;
    NSString *filePath = [NSString stringWithFormat:@"%@/%@",photoCacheDir,fileName];   // 保存文件的名称
    [UIImagePNGRepresentation(image) writeToFile: filePath    atomically:YES];
    
    return [NSString stringWithFormat:@"%@/%@",date,fileName];
}


+(NSString*) getImageLocalPath:(NSString*)imagepath
{
     NSArray *paths = DOCUMENT_PATH;
    return   [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",folerName_PHOTO,imagepath]];
}

+(NSString*) priceToStr:(double)price
{
    return [NSString stringWithFormat:@"%0.2f",price];
}

+(NSString*) numberToStr:(double)number;
{
    return [NSString stringWithFormat:@"%0.0f",number];
}

+ (UIImage *)snapshot:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}
@end
